package com.esm.sm800.database.manager;

import com.esm.sm800.activity.AppContext;
import com.esm.sm800.database.dbmodel.BornDoor;
import com.esm.sm800.database.greendao.BornDoorDao;

import java.util.ArrayList;

public class BorndoorCenter {

    private static BorndoorCenter instance = new BorndoorCenter();

    public static BorndoorCenter getInstance() {
        return instance;
    }

     public BornDoor getBornDoor(String name) {
        BornDoor BornDoor = null;
        try {
            BornDoorDao BornDoorDao = AppContext.getInstances().getDaoSession().getBornDoorDao();
            ArrayList<BornDoor> list = new ArrayList<BornDoor>();
            list = (ArrayList) BornDoorDao.queryBuilder()
                    .where(com.esm.sm800.database.greendao.BornDoorDao.Properties.Name.eq(name))
                    .build().list();
            if (list.size() > 0) {
                BornDoor = list.get(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return BornDoor;
    }

    public Long saveBornDoor(BornDoor BornDoor) {
        if (BornDoor == null) {
            return 0L;
        }

        BornDoor a = getBornDoor(BornDoor.getName());
        if (a != null) {
            BornDoor.setId(a.getId());
        }

        try {
            BornDoorDao BornDoorDao = AppContext.getInstances().getDaoSession().getBornDoorDao();
            long id = BornDoorDao.insertOrReplace(BornDoor);
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0L;
    }

    public Long insertBornDoor(BornDoor BornDoor) {
        if (BornDoor == null) {
            return 0L;
        }

        try {
            BornDoorDao BornDoorDao = AppContext.getInstances().getDaoSession().getBornDoorDao();
            long id = BornDoorDao.insertOrReplace(BornDoor);
            return id;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return 0L;
    }

    public ArrayList<BornDoor> getBornDoorlist() {
        ArrayList<BornDoor> list = new ArrayList<BornDoor>();
        try {
            BornDoorDao BornDoorDao = AppContext.getInstances().getDaoSession().getBornDoorDao();
            list = (ArrayList) BornDoorDao.queryBuilder().build().list();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }
}
